/**
 * Copyright (C) 2011-2015 The XDocReport Team <xdocreport@googlegroups.com>
 *
 * All rights reserved.
 *
 * Permission is hereby granted, free  of charge, to any person obtaining
 * a  copy  of this  software  and  associated  documentation files  (the
 * "Software"), to  deal in  the Software without  restriction, including
 * without limitation  the rights to  use, copy, modify,  merge, publish,
 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
 * permit persons to whom the Software  is furnished to do so, subject to
 * the following conditions:
 *
 * The  above  copyright  notice  and  this permission  notice  shall  be
 * included in all copies or substantial portions of the Software.
 *
 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package fr.opensagres.xdocreport.document.images;

import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;


public class ImageInfoTestCase {

    @Test
    public void testToRecognizedAJpegExifImageWithImageInfo() throws Exception {
        InputStream in = ImageInfoTestCase.class.getResourceAsStream("jpeg-exif.jpg");
        IImageInfo info = loadImageInfo(in);
        assertThat(info.getMimeType().getType(), is("jpeg"));
        assertThat(info.getHeight(), is(1536));
        assertThat(info.getWidth(), is(2048));
        checkIfStreamIsClosed(in);
    }

    @Test
    public void testToRecognizedAJpegJFIFImageWithImageInfo() throws Exception {
        InputStream in = ImageInfoTestCase.class.getResourceAsStream("jpeg-jfif.jpg");
        IImageInfo info = loadImageInfo(in);
        assertThat(info.getMimeType().getType(), is("jpeg"));
        assertThat(info.getHeight(), is(1536));
        assertThat(info.getWidth(), is(2048));
        checkIfStreamIsClosed(in);
    }

    private IImageInfo loadImageInfo(InputStream is) throws IOException {
        SimpleImageInfo imageInfo = new SimpleImageInfo();
        imageInfo.setInput(is);
        imageInfo.check();
        return imageInfo;
    }

    private void checkIfStreamIsClosed(InputStream is) throws IOException {
        try {
            is.read();
            Assert.fail();
        } catch (IOException e) {
        }
    }
}
